package com.hks360.car_treasure.fragment.control;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.hks360.car_treasure.R;
import com.hks360.car_treasure.common.AppConfig;
import com.hks360.car_treasure.common.Method;
import com.hks360.car_treasure.common.PrefKey;
import com.hks360.car_treasure.fragment.BaseFragment;
import com.hks360.car_treasure.model.CarLocation;
import com.hks360.car_treasure.model.CarState;
import com.hks360.car_treasure.mvp.equipment.presenter.EquipmentPresenter;
import com.hks360.car_treasure.mvp.equipment.presenter.EquipmentPresenterImpl;
import com.hks360.car_treasure.mvp.equipment.view.EquipmentView;
import com.hks360.car_treasure.util.LogUtil;
import com.hks360.car_treasure.util.MyUtil;
import com.hks360.car_treasure.widget.ArcSeekbarView;
import com.hks360.car_treasure.widget.FourClickView;
import com.hks360.library.util.CommonUtil;
import com.hks360.library.util.PrefUtil;
import com.hks360.library.util.UIUtil;
import com.hks360.library.widget.CheckImageView;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 控制Fragment
 */
public class ControlFragment_circle extends BaseFragment implements EquipmentView {
    private static final int TASK_GET_STATUS = 1;
    private static final int TASK_SEND_COMMAND = 2;
    private String mTboxId;

    private CheckImageView mEngineCiv;
    private CheckImageView mLockCiv;
    private CheckImageView mFindCarCiv;
    private CheckImageView mFrontFrostCiv;
    private CheckImageView mRearFrostCiv;
    private CheckImageView mSearHeatCiv;
    private CheckImageView mOpenColdCiv;
    private FourClickView mControlTempFcv;
    private ArcSeekbarView mTempAsbv;

    private boolean mEngineClick = false;
    private boolean mLockClick = false;
    private boolean mFindCarClick = false;
    private boolean mFrontFrostClick = false;
    private boolean mRearFrostClick = false;
    private boolean mSearHeatClick = false;
    private boolean mOpenColdClick = false;

    private ImageView mHandBrakeIv;
    private ImageView mGearIv;
    private ImageView mLightIv;
    private ImageView mCenterControlIv;
    private ImageView mDoorIv;
    private ImageView mPower;
    Timer timer;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            initData();
        }
    };
    private boolean isFirst = true;
    private boolean isGet = true;

    private EquipmentPresenter mEquipmentPresenter;

    public void initView() {
        initTitleBar(R.string.title_control);
        initDialog();
        setupView();
        initData();
        addListener();
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(0);
            }
        }, 0, 2000);
    }
    @Override
    public void onPause() {
        super.onPause();
        if (timer != null) {
            timer.cancel();
        }
        isFirst = true;
        isGet = true;
    }

    @Override
    public void setupView() {
        mEngineCiv = UIUtil.findViewById(mRootView, R.id.control_engine_civ);
        mLockCiv = UIUtil.findViewById(mRootView, R.id.control_lock_civ);
        mFindCarCiv = UIUtil.findViewById(mRootView, R.id.control_findcar_civ);
        mFrontFrostCiv = UIUtil.findViewById(mRootView, R.id.control_frontdefrost_civ);
        mRearFrostCiv = UIUtil.findViewById(mRootView, R.id.control_reardefrost_civ);
        mSearHeatCiv = UIUtil.findViewById(mRootView, R.id.control_seatheat_civ);
        mOpenColdCiv = UIUtil.findViewById(mRootView, R.id.control_opencold_civ);
        mControlTempFcv = UIUtil.findViewById(mRootView, R.id.control_temperature_fcv);
        mTempAsbv = UIUtil.findViewById(mRootView, R.id.control_temperature_asbv);
        mHandBrakeIv = UIUtil.findViewById(mRootView, R.id.handbrake);
        mGearIv = UIUtil.findViewById(mRootView, R.id.gear);
        mLightIv = UIUtil.findViewById(mRootView, R.id.light);
        mCenterControlIv = UIUtil.findViewById(mRootView, R.id.centercontrol);
        mDoorIv = UIUtil.findViewById(mRootView, R.id.door);
        mPower = UIUtil.findViewById(mRootView, R.id.power);
    }

    @Override
    public void getCurrentStateSuccess(CarState carState) {
        if (carState == null) {
            if (isFirst) {
                CommonUtil.showToast(getContext(), "未获取到正确的数据!");
                isFirst = false;
            }
            return;
        }
        if (isGet) {
            CommonUtil.showToast(getContext(), "已获取到正确的数据!");
            isGet = false;
        }
        //风速设置
        switch (carState.getWindSpeed()) {
            case "000":
                mControlTempFcv.setFlag(0);
                break;
            case "001":
                mControlTempFcv.setFlag(1);
                break;
            case "011":
                mControlTempFcv.setFlag(2);
                break;
            case "100":
                mControlTempFcv.setFlag(3);
                break;
        }
        //信号状态
        //电量状态
        if (Integer.parseInt(carState.getPower()) > 75) {//75-100
            mPower.setImageResource(R.drawable.power100);
        } else if (Integer.parseInt(carState.getPower()) > 50) {//50-75
            mPower.setImageResource(R.drawable.power75);
        } else if (Integer.parseInt(carState.getPower()) > 25) {//25-50
            mPower.setImageResource(R.drawable.power50);
        } else if (Integer.parseInt(carState.getPower()) > 10) {//10-25
            mPower.setImageResource(R.drawable.power10);
        } else if (Integer.parseInt(carState.getPower()) >= 0) {//0-10
            mPower.setImageResource(R.drawable.power0);
        }
        //灯光状态
//        switch (carState.getLight().charAt(0) + "") {
//            case "0":
//                mLightIv.setImageResource(R.mipmap.light_close);
//                break;
//            case "1":
//                mLightIv.setImageResource(R.drawable.light_open);
//                break;
//        }
////        左前右前 左后右后  车门状态
////        0 00000 00
//        if (carState.getLock().substring(0, 1).equals("0")) {
//            mCenterControlIv.setImageResource(R.drawable.center_control_lock);
//        } else {
//            mCenterControlIv.setImageResource(R.drawable.center_control);
//        }
//        switch (carState.getLock().substring(1, 6)) {
//            case "00000":
//                mDoorIv.setImageResource(R.drawable.door_all_close);
//                break;
//            case "00011"://右开后开
//            case "00111":
//            case "00101":
//                mDoorIv.setImageResource(R.drawable.door_right_tail);
//                break;
//            case "00010"://右开后关
//            case "00110":
//            case "00100":
//                mDoorIv.setImageResource(R.drawable.door_right);
//                break;
//            case "01001"://左开后开
//            case "10001":
//            case "11001":
//                mDoorIv.setImageResource(R.drawable.door_left_tail);
//                break;
//            case "01000"://左开
//            case "10000":
//            case "11000":
//                mDoorIv.setImageResource(R.drawable.door_left);
//                break;
//            case "01011"://左开右开后开
//            case "10101":
//            case "10011":
//            case "01101":
//            case "01111":
//            case "10111":
//            case "11011":
//            case "11101":
//            case "11111":
//                mDoorIv.setImageResource(R.drawable.door_all_open);
//                break;
//            case "01010"://左开右开后关
//            case "10100":
//            case "10010":
//            case "01100":
//            case "01110":
//            case "10110":
//            case "11010":
//            case "11100":
//            case "11110":
//                mDoorIv.setImageResource(R.drawable.door_right_left);
//                break;
//            case "00001"://后开
//                mDoorIv.setImageResource(R.drawable.door_tail);
//                break;
//
//        }
//
//
//        //档位设置
//        switch (carState.getGear()) {
//            case "00":
//            case "10":
//                mGearIv.setImageResource(R.drawable.nullgear);
//                break;
//            case "01":
//                mGearIv.setImageResource(R.drawable.drive);
//                break;
//            case "11":
//                mGearIv.setImageResource(R.drawable.reverse);
//                break;
//        }
//        //手刹状态
//        switch (carState.getHandBrake()) {
//            case "0":
//                mHandBrakeIv.setImageResource(R.drawable.brake_normal);
//                break;
//            case "1":
//                mHandBrakeIv.setImageResource(R.drawable.brake_up);
//                break;
//        }
//        //引擎状态
//        switch (carState.getEngine()) {
//            case "0":
//                mEngineClick = false;
//                break;
//            case "1":
//                mEngineClick = true;
//                break;
//        }
        //车锁状态
        switch (carState.getLock().substring(0, 1)) {
            case "0":
                mLockClick = false;
                break;
            default:
                mLockClick = true;
                break;
        }
        //前除霜状态
        switch (carState.getFrontDefrost()) {
            case "0":
                mFrontFrostClick = false;
                break;
            case "1":
                mFrontFrostClick = true;
                break;
        }
        //后除霜状态
        switch (carState.getRearDefrost()) {
            case "0":
                mRearFrostClick = false;
                break;
            case "1":
                mRearFrostClick = true;
                break;
        }
        //座椅加热状态
        switch (carState.getSeatHeating()) {
            case "000":
                mSearHeatClick = false;
                break;
            default:
                mSearHeatClick = true;
                break;
        }
        switch (carState.getACState()) {
            case "0":
                mOpenColdClick = false;
                mControlTempFcv.setFlag(0);
                break;
            default:
                mOpenColdClick = true;
                break;
        }

        initState();
    }

    private void initState() {
        mEngineCiv.setChecked(mEngineClick);
        mLockCiv.setChecked(mLockClick);
        mFindCarCiv.setChecked(mFindCarClick);
        mFrontFrostCiv.setChecked(mFrontFrostClick);
        mRearFrostCiv.setChecked(mRearFrostClick);
        mSearHeatCiv.setChecked(mSearHeatClick);
        mOpenColdCiv.setChecked(mOpenColdClick);
        mControlTempFcv.setClick(mOpenColdClick);
    }

    @Override
    public void initData() {
        mTboxId = Method.getTboxId();
        mEquipmentPresenter = new EquipmentPresenterImpl(getContext(), this);
        mEquipmentPresenter.getCurrentState(TASK_GET_STATUS);
        initState();
    }

    @Override
    public void addListener() {
        mEngineCiv.setOnClickListener(this);
        mLockCiv.setOnClickListener(this);
        mFindCarCiv.setOnClickListener(this);
        mFrontFrostCiv.setOnClickListener(this);
        mRearFrostCiv.setOnClickListener(this);
        mSearHeatCiv.setOnClickListener(this);
        mOpenColdCiv.setOnClickListener(this);
        mControlTempFcv.setOnFlagChangedListener(new MyOnFlagChangedListener());
        mTempAsbv.setOnProgressChangedListener(new MyProgressChangedListener());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_control_circle, null);
        initView();
        return mRootView;
    }

    @Override
    public void viewClick(View v) {
        switch (v.getId()) {
            case R.id.control_engine_civ:
                mEngineClick = !mEngineClick;
                mEngineCiv.setChecked(mEngineClick);
                mEquipmentPresenter.sendCommand(TASK_SEND_COMMAND, MyUtil.getCommandEngin(mEngineClick));
                break;
            case R.id.control_lock_civ:
                if (clickInvalid()) break;
                mLockClick = !mLockClick;
                mLockCiv.setChecked(mLockClick);
                mEquipmentPresenter.sendCommand(TASK_SEND_COMMAND, MyUtil.getCommandLock(mLockClick));
                break;
            case R.id.control_findcar_civ:
                mFindCarClick = !mFindCarClick;
                mFindCarCiv.setChecked(mFindCarClick);
                if (mFindCarClick) {
                    mEquipmentPresenter.sendCommand(TASK_SEND_COMMAND, "LookForCar");
                }
                break;
            case R.id.control_frontdefrost_civ:
                if (clickInvalid()) break;
                mFrontFrostClick = !mFrontFrostClick;
                mFrontFrostCiv.setChecked(mFrontFrostClick);
                mEquipmentPresenter.sendCommand(TASK_SEND_COMMAND, MyUtil.getCommandFrontDefrost(mFrontFrostClick));
                break;
            case R.id.control_reardefrost_civ:
                if (clickInvalid()) break;
                mRearFrostClick = !mRearFrostClick;
                mRearFrostCiv.setChecked(mRearFrostClick);
                mEquipmentPresenter.sendCommand(TASK_SEND_COMMAND, MyUtil.getCommandRearDefrost(mRearFrostClick));
                break;
            case R.id.control_seatheat_civ:
                if (clickInvalid()) break;
                mSearHeatClick = !mSearHeatClick;
                mSearHeatCiv.setChecked(mSearHeatClick);
                mEquipmentPresenter.sendCommand(TASK_SEND_COMMAND, MyUtil.getCommandSeatHeat(mSearHeatClick));
                break;
            case R.id.control_opencold_civ:
                if (clickInvalid()) break;
                mOpenColdClick = !mOpenColdClick;
                mOpenColdCiv.setChecked(mOpenColdClick);
                mControlTempFcv.setClick(mOpenColdClick);
                mEquipmentPresenter.sendCommand(TASK_SEND_COMMAND, MyUtil.getCommandSeatHeat(mOpenColdClick));
                break;
        }
    }

    private boolean clickInvalid() {
        if (mEngineCiv.ismInvalid()) {
            return false;
        }
        CommonUtil.showToast(getContext(), "请先打开Engine再点击按钮");
        return true;
    }

    @Override
    public void showLoading() {
        mDialog.show();
    }

    @Override
    public void hideLoading() {
        mDialog.dismiss();
    }

    @Override
    public String getMobile() {
        return null;
    }

    @Override
    public String getVin() {
        return null;
    }

    @Override
    public String getTboxId() {
        return PrefUtil.getString(getContext(), PrefKey.TBOX_ID, "");
    }

    @Override
    public void bindUserSuccess() {

    }


    @Override
    public void showToastMsg(String msg) {
        CommonUtil.showToast(getContext(), msg);
    }

    @Override
    public void readCurrentLocationSuccess(CarLocation carLocation) {

    }

    @Override
    public void readCurrentLocationFailed() {

    }

    private class MyOnFlagChangedListener implements FourClickView.OnFlagChangedListener {

        @Override
        public void onFlagChanged(int flag) {
            if (mControlTempFcv.getFlag() != 0) {
//                mEquipmentPresenter.sendCommand(TASK_SEND_COMMAND, MyUtil.getCommandAc(mOpenColdClick, mControlTempFcv.getFlag()));
            }

        }
    }

    private class MyProgressChangedListener implements ArcSeekbarView.OnProgressChangedListener {

        @Override
        public void onProgressChanged(int progress) {
            LogUtil.d("progress:" + progress);
            mEquipmentPresenter.sendCommand(TASK_SEND_COMMAND, "AUTOACON" + progress);
        }
    }
}
